//业务对象
//主题业务对象 theme

import {Http} from "../utils/http";
import {server_config} from "../config/config";

class Theme {
    static theme1 = 't-1';
    static theme2 = 't-2';
    static theme3 = 't-3';
    static theme4 = 't-4';
    static with_spu = 't-4';
    //定义一个包含所有theme的数组
    themes = [];
    //使用静态方法，不需要实例化Theme对象
    //优化回调嵌套，使用使用 promise 链式操作，使异步代码看起来如同步般易读，从回调地狱中解脱出来
    async getTopTheme1() {
        return await Http.request({
            url: `${server_config.themes}`,
            data: {
                names: `${Theme.theme1}`
            }
        });
    }

    async getAllTheme() {
        const names = `${Theme.theme1},${Theme.theme2},${Theme.theme3},${Theme.theme4}`;
        const resultThemes = await Http.request({
            url: `${server_config.themes}`,
            data: {
                names: names
            }
        });
        this.themes = resultThemes.data;
    }

    /**
     *筛选出相对应的想要的theme
     */
    getTheme1() {
        return this.themes.find(t => t.name === Theme.theme1);
    }

    getTheme2() {
        return this.themes.find(t => t.name === Theme.theme2);
    }

    getTheme4() {
        return this.themes.find(t => t.name === Theme.theme4);
    }

    async getTheme2Spu() {
        return this.getThemWithSpu(Theme.theme2);
    }

    /**
     * 获取带有Spu的主题
     * @returns {Promise<void>}
     */
    async getThemWithSpu(name) {
        return await Http.request({
            url: `theme/name/${name}/with_spu`
        });
    }

}

export {
    Theme
}